<?php

namespace app\common\service;

use think\Service;
use app\common\library\Auth;

/**
 * 认证服务
 */
class Socket extends Service
{
    public function __construct($app)
    {
        //读取swoole配置
        parent::__construct($app);
    }


    public static function sendMsg($message,$fd){
        $url = "http://".config("swoole.socket_host").":".config("swoole.swoole_port")."/";
        //构建信息
        $data = array(
            "action" => "system_send_message",
            "message" => $message,
            "fd" => $fd,
        );
        //发送日志
        curl_data($data,$url);
        return true;
    }


    public static function sendMsgSocket($message,$fd){
        $config = config("swoole.");
        $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
        $result = socket_connect($socket,config("swoole.socket_host"),config("swoole.swoole_port"));
        if(!$result){
            //写日志
            return false;
        }
        //构建信息
        $data = array(
            "action" => "system_send_message",
            "message" => $message,
            "fd" => $fd
        );
        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
        //发送日志
        socket_write($socket,$data,strlen($data));
        //关闭连接
        socket_close($socket);
        return true;
    }


}
